package battleship.player;

import battleship.Loop;
import battleship.PluginAI;
import battleship.Setup;
import battleship.field.Field;
import battleship.loop.MoveCollisionException;
import battleship.loop.OutOfSpaceException;

public class Player {
	private String name;
	private boolean bot;
	private Field playField;
	private PluginAI ai;
	
	
	public Player(String name, boolean bot){
		this.name = name;
		this.bot = bot;
		playField = new Field();
	}
	
	public void setAI(PluginAI ai)
	{
		this.ai = ai;
	}
	
	public PluginAI getAI()
	{
		return ai;
	}
	
	public void setField(Field field)
	{
		this.playField = field;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return name;
	}
	
	public boolean isBot(){
		return bot;
	}
	
	public void setBot(boolean bot){
		this.bot = bot;
	}

	public Field getField(){
		return playField;
	}
	
	
	/**
	 * strike the opponent's field
	 * and sets the struk position on the opponent's field
	 * using a humanly readable string
	 * 
	 * @param position the string format of the position (e.g. a1)
	 * @return true if hit, false if missed
	 * @throws MoveCollisionException this position has already been shot
	 * @throws OutOfSpaceException this position is not inside of the field
	 */
	public boolean strike(String position) throws MoveCollisionException, OutOfSpaceException, IllegalArgumentException
	{ //TODO use this somewhere
		String xVal; // 1
		String yVal; // a
		
		int y = -1;
		int x = -1;
		
		yVal = String.valueOf(position.charAt(0));
		
		for(int alphabet = 0; alphabet < Setup.getPositionMap().length; alphabet++)
		{
			if(yVal.equalsIgnoreCase(Setup.getPositionMap()[alphabet]))
			{
				y = alphabet;
				break;
			}
		}
		
		if (y == -1)
		{
			throw new IllegalArgumentException();
		}
		
		xVal = position.substring(1);
		
		x = Integer.parseInt(xVal);
		
		int realX = x - 1;
		int realY = y;
		
		
		moveCollision(new int[] { realY, realX });
		
		return strike(realY, realX);
	}
	
	/**
	 * strike the opponent's field using x and y points
	 * and sets the struk point on the opponent's field
	 * @param y the y position to strike
	 * @param x the x position to strike
	 * @return true if hit, false if missed
	 * @throws MoveCollisionException this position has already been shot
	 * @throws OutOfSpaceException this position is not inside of the field
	 */
	public boolean strike(int y, int x) throws MoveCollisionException, OutOfSpaceException
	{
		Loop.setLastTurn(this);
		
		moveCollision(new int[] { y, x});
		Player player2;
		
		player2 = Setup.getPlayer2(this);
		
		player2.getField().getField()[y][x] = true;
		
		if (player2.getField().getHit(new int[] { y , x }))
		{
			return true;
		}
		
		return false;
	}
	
	
	public void moveCollision(int position[]) throws MoveCollisionException, OutOfSpaceException{ 
		//checks if the move given by the user hasn't already been made
		Player player2;
		
		/* check if the value is inside our bounds */
		
		if(position[0] >= Field.getHeight() || position[1] >= Field.getWidth())
		{
			throw new OutOfSpaceException();
		}

		/* setting the player2 value (player getting attacked */

		if(this == Setup.getPlayers()[0]){
			player2 = Setup.getPlayers()[1];
		}
		else {
			player2 = Setup.getPlayers()[0];
		}
		/* getting the field of the player he is aiming for */

		boolean[][] field = player2.getField().getField();

		if(field[position[0]][position[1]] == true){
			throw new MoveCollisionException();
		}
	}
}
